/**********************************************************************************************************************
 *
 * Project:    mGameEngine
 * File:       window.cpp
 * Author:     karooolek
 * Created on: 2009-01-06
 *
 **********************************************************************************************************************/

#include "window.h"

namespace mGameEngine
{
namespace System
{

String Window::_title;
Vector2 Window::_size;
bool Window::_fullScreen;
uint Window::_frames;

void Window::_glutReshapeFunc(int width, int height)
{
    _size.x = width;
    _size.y = height;

    glViewport(0, 0, width, height);
}

void Window::setTitle(const String &title)
{
    _title = title;
    glutSetWindowTitle(title);
}

void Window::setSize(const Vector2 & size)
{
    _size = size;
    glutReshapeWindow(static_cast<int>(_size.x), static_cast<int>(_size.y));
}

float Window::getAspectRatio()
{
    return _size.x / _size.y;
}

void Window::setFullScreen(bool fullScreen)
{
    // requested mode already set
    if(fullScreen == _fullScreen)
    {
        return;
    }

    // set requested mode
    _fullScreen = fullScreen;

    if(_fullScreen)
    {
        // TODO save window size and coordinates
        glutFullScreen();
    }
    else
    {
        // TODO restore window size and coordinates
        glutReshapeWindow(300, 300);
        glutPositionWindow(100, 100);
    }
}

float Window::getFramesPerSecond()
{
    static Timer timer(0.0f, true);
    static float fps = 0.0f;

    float t = timer.getTime();
    if(t > 0.5f)
    {
        fps = float(_frames) / t;
        _frames = 0;
        timer.reset();
    }

    return fps;
}

}
}
